﻿using ClassEntities;
using Facade;
using Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace Edu_Examination
{
    public partial class frm_login : Form
    {

        public frm_login()
        {
            InitializeComponent();
            //DataTable dt = new DataTable();
            //dt.Columns.Add("ip", typeof(string));
            //dt.Columns.Add("port", typeof(string));
            //dt.TableName = "config";
            //DataRow dr = dt.NewRow();
            //dr["ip"] = "192.168.0.101";
            //dr["port"] = "7777";
            //dt.Rows.Add(dr);
            //dt.WriteXml(AppDomain.CurrentDomain.BaseDirectory + "server.xml", XmlWriteMode.WriteSchema, false);
            loadEvent();
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "server.xml"))
            {
                DataTable dt = new DataTable();
                dt.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "server.xml");
                if (dt.Rows.Count > 0)
                {
                    name = "tcp://" + dt.Rows[0]["ip"] + ":" + dt.Rows[0]["port"] + "/";
                    backgroundWorker1.RunWorkerAsync();
                }
            }

        }
        int n = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy) { Console.WriteLine("run"); n = 0; backgroundWorker1.RunWorkerAsync(); }
            else
            {
                n++;
                this.pictureBox2.Image = global::Edu_Examination.Properties.Resources.status_off;
                if (n > 5)
                {
                    n = 0;
                }
                //Console.WriteLine("backgroundWorker1.IsBusy");
            }
        }
        string name = null;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            connect();
        }
        SuperFacade facade { get; set; }
        bool connected = false;
        public void connect()
        {
            Lib.doConnection.url = name;
            facade = Lib.doConnection.SuperFacade;
            try { facade.Ping(); connected = true; }
            catch { connected = false; }
        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //if (connected)
            //{
            //    this.pictureBox2.Image = global::Edu_Examination.Properties.Resources.status_Onl;
            //    main_panel.Enabled = true;
            //}
            //else
            //{
            //    this.pictureBox2.Image = global::Edu_Examination.Properties.Resources.status_off;
            //    main_panel.Enabled = false;
            //}
        }
        public void loadEvent()
        {
            this.txtEmail.Enter += new System.EventHandler(this.txtEnter);
            this.txtPassword.Enter += new System.EventHandler(this.txtEnter);

            this.txtEmail.Leave += new System.EventHandler(this.txtLeave);
            this.txtPassword.Leave += new System.EventHandler(this.txtLeave);

            this.lblEmail.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MouseClick_Event);
            this.lblPasswork.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MouseClick_Event);
        }

        private void MouseClick_Event(object sender, MouseEventArgs e)
        {
            Label lbl = (Label)sender;
            switch (lbl.Name)
            {
                case "lblEmail":
                    setFocus(true);
                    break;
                case "lblPasswork":
                    setFocus(false);
                    break;
                default:
                    setFocus(true);
                    break;
            }
        }
        // load textbox Leave Event
        private void txtLeave(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            switch (txt.Name)
            {
                case "txtEmail":
                    if (lblEmail.Visible == false && string.IsNullOrEmpty(txtEmail.Text.Trim())) lblEmail.Visible = true;
                    break;
                case "txtPassword":
                    if (lblPasswork.Visible == false && string.IsNullOrEmpty(txtPassword.Text.Trim())) lblPasswork.Visible = true;
                    break;
                default:
                    if (lblEmail.Visible == false && string.IsNullOrEmpty(txtEmail.Text.Trim())) lblEmail.Visible = true;
                    break;
            }
        }
        // load textbox enter event
        private void txtEnter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            switch (txt.Name)
            {
                case "txtEmail":
                    if (lblEmail.Visible == true && string.IsNullOrEmpty(txtEmail.Text.Trim())) lblEmail.Visible = false;
                    break;
                case "txtPassword":
                    if (lblPasswork.Visible == true && string.IsNullOrEmpty(txtPassword.Text.Trim())) lblPasswork.Visible = false;
                    break;
                default:
                    if (lblEmail.Visible == true && string.IsNullOrEmpty(txtEmail.Text.Trim())) lblEmail.Visible = false;
                    break;
            }

        }

        public void setFocus(bool _bool)
        {
            if (_bool)
            {
                if (lblEmail.Visible == true)
                {
                    if (lblPasswork.Visible == false && string.IsNullOrEmpty(txtPassword.Text.Trim())) { lblPasswork.Visible = true; }
                    lblEmail.Visible = false;
                    txtEmail.Clear();
                    txtEmail.Focus();
                }
            }
            else
            {
                if (lblPasswork.Visible == true)
                {
                    if (lblEmail.Visible == false && string.IsNullOrEmpty(txtEmail.Text.Trim())) { lblEmail.Visible = true; }
                    lblPasswork.Visible = false;
                    txtPassword.Clear();
                    txtPassword.Focus();
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Vui lòng liên hệ với quản trị của trường để thay đổi mật khẩu của bạn.");
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            if (!connected) connect();
            Hashtable hash = new Hashtable();
            hash["maSV"] = txtEmail.Text;
            hash["pass"] = txtPassword.Text;
            tblSinhVien bean = facade.SinhVienLogin(hash);
            if (bean != null)
            {
                Bean = bean;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("false");
            }
        }

        public tblSinhVien Bean { get; set; }
    }
}
